package com.suda.sudaexamsystem.controller;

import com.suda.sudaexamsystem.dto.UserDto;
import com.suda.sudaexamsystem.pojo.Clazz;
import com.suda.sudaexamsystem.pojo.User;
import com.suda.sudaexamsystem.service.ClazzService;
import com.suda.sudaexamsystem.service.RequestService;
import com.suda.sudaexamsystem.service.UserService;
import com.suda.sudaexamsystem.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/request")
public class RequestController {
    @Autowired
    private RequestService requestService;
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private UserService userService;

    // 查看当前班级的申请
    //{"msg":"操作成功","code":200,"data":[{"id":2,"name":"update","login_name":"李莫愁","sex":"女"},
    // {"id":3,"name":"link","login_name":"林克","sex":"男"}]}
    @GetMapping(value = "/{clazzId}", produces = {"application/json;charset=UTF-8"})
    public AjaxResult getRequests(@PathVariable("clazzId") int clazzId) {
        List<User> userList = requestService.findByClazzId(clazzId);
        List<UserDto> userDtoList = new ArrayList<>();
        for (User user : userList) {
            userDtoList.add(user.toDto());
        }
        return AjaxResult.success(userDtoList);
    }

    // 申请加入班级
    @PostMapping(value = "/{clazzId}/{userId}", produces = {"application/json;charset=UTF-8"})
    public AjaxResult sendRequest(@PathVariable("clazzId") int clazzId, @PathVariable("userId") int userId) {
        requestService.sendApplyRequest(userId, clazzId);
        return AjaxResult.success();
    }

    // 同意加入
    @PostMapping(value = "/access/{clazzId}/{userId}", produces = {"application/json;charset=UTF-8"})
    public AjaxResult accessRequest(@PathVariable("clazzId") int clazzId, @PathVariable("userId") int userId) {
        User user = userService.findById(userId);
        if (user == null) {
            return AjaxResult.error("用户不存在");
        }
        Clazz clazz = clazzService.findById(clazzId);
        if (clazz == null) {
            return AjaxResult.error("班级不存在");
        }
        if (userService.isClazzContainsUser(user.getId(), clazzId)) {
            return AjaxResult.error("用户已经在班级中了");
        }
        clazzService.addUserToClazz(user, clazz);
        return AjaxResult.success();
    }

    // 不同意加入
    @PostMapping(value = "/refuse/{clazzId}/{userId}", produces = {"application/json;charset=UTF-8"})
    public AjaxResult refuseRequest(@PathVariable("clazzId") int clazzId, @PathVariable("userId") int userId) {
        User user = userService.findById(userId);
        if (user == null) {
            return AjaxResult.error("用户不存在");
        }
        Clazz clazz = clazzService.findById(clazzId);
        if (clazz == null) {
            return AjaxResult.error("班级不存在");
        }
        requestService.deleteApplyRequest(user.getId(), clazz.getId());
        return AjaxResult.success();
    }
}
